class H2O {
public:
    atomic<int> hnum = 0;
    condition_variable condc, condv;
    mutex mtx;
    H2O() {
        hnum = 0;
    }

    void hydrogen(function<void()> releaseHydrogen) {
        unique_lock<mutex> lock(mtx);
        {
            condc.wait(lock, [&] { return hnum < 2; });
            ++hnum;
            releaseHydrogen();
            if(hnum == 2) condv.notify_one();
        }
        
    }

    void oxygen(function<void()> releaseOxygen) {
        unique_lock<mutex> lock(mtx);
        condv.wait(lock, [&]{ return hnum == 2; });
        hnum = 0;
        releaseOxygen();
        condc.notify_all();
    }
};